package com.sumainfor.wx.tenant.controller;

import com.alibaba.fastjson.JSONObject;
import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.wx.tenant.feign.DevelopmentFeignClient;
import com.sumainfor.wx.tenant.service.IDinFormationService;
import com.sumainfor.wx.tenant.service.impl.TenantIdentitService;
import com.sumainfor.wx.tenant.util.HttpUtil;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 个人中心的证件信息
 *
 * @author xiaoma
 */
@Controller
@RequestMapping("IDinFormation")
public class IDinFormationColler {


    @Autowired
    IDinFormationService ifationservice;

    @Autowired
    DevelopmentFeignClient developmentFeignClient;

    @Autowired
    TenantIdentitService TenantIdentitService;

    @ResponseBody
    @PostMapping("/identity")
    public JsonResult identity(@org.springframework.web.bind.annotation.RequestBody Map<String, Object> params) {
        return TenantIdentitService.identity(params);
    }


    /**
     * 阿里云身份证识别OCR
     *
     * @param params
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("getIdendo")
    public Map<String, Object> getIdendo(@org.springframework.web.bind.annotation.RequestBody Map<String, String> params) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            String host = "https://cardiddecode.market.alicloudapi.com/api/decode_cardid_aliyun";
            String appcode = "c8a411a9ba8642cf8353f289558fa505";
            if (params.get("imgbase64") != null) {

                String img = params.get("imgbase64").toString();

                String bodys = "{\"imgbase64\":\"" + img + "\"}";

                MediaType JSON = MediaType.parse("application/json; charset=utf-8");
                RequestBody body = okhttp3.RequestBody.create(JSON, bodys);

                /**
                 * 重要提示如下:
                 * HttpUtils请从
                 * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
                 * 下载
                 *
                 * 相应的依赖请参照
                 * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
                 */
                Response response = HttpUtil.doPostForOCR(host, body).execute();
                if (!response.isSuccessful()) {
                    result.put("code", 400);
                    result.put("msg", "服务器繁忙...");
                    return result;
                }
                //System.out.println(response.toString());
                //获取response的body
                String str = response.body().string();
                response.close();
                Map<String, Object> map = JSONObject.parseObject(str, Map.class);
                if (map.get("code").toString().equals("200")) {
                    result.put("code", 0);
                    result.put("data", map.get("data"));
                } else {
                    result.put("code", 400);
                    result.put("msg", map.get("desc"));
                }

            } else {

                result.put("code", 400);
                result.put("msg", "请输入必传参数");
            }


        } catch (Exception e) {

        }

        return result;
    }


    /**
     * 查询个人证件信息
     *
     * @param params
     * @return
     */
    @ResponseBody
    @RequestMapping("getIDinfor")
    public JsonResult getIDinfor(@RequestParam Map<String, Object> params) {

        return ifationservice.getIDinfor(params);

    }


    /**
     * 四要素验证 + 更新信息
     *
     * @param params
     * @return
     */
    @PostMapping("/checkAccountElement")
    @ResponseBody
    public JsonResult checkAccountElement(@org.springframework.web.bind.annotation.RequestBody Map<String, Object> params) {
//        if (!FieldCheckUtil.isMapFieldNotNull(params, "idno", "name", "mobile", "cardno","Frontphoto","Frontpside","Belongsbank")) {
//            return JsonResult.errorForEmpty();
//        }
       return ifationservice.checkAccountElement(params);
    }


    /**
     * 绑定身份信息
     *
     * @param params
     * @return
     */
    @ResponseBody
    @RequestMapping("savaidentit")
    public JsonResult savaidentit(@org.springframework.web.bind.annotation.RequestBody Map<String, Object> params) {

        return ifationservice.savaidentit(params);
    }

    /**
     * 新增或者修改工作信息
     *
     * @param params
     * @return
     */
    @ResponseBody
    @RequestMapping("savework")
    public JsonResult savework(@org.springframework.web.bind.annotation.RequestBody Map<String, Object> params) {

        return ifationservice.savework(params);
    }

    /**
     * 查询行业方向信息
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("getindirection")
    public JsonResult getindirection() {

        return ifationservice.getindirection();
    }

    /**
     * 查询个人工作信息
     *
     * @param params
     * @return
     */
    @ResponseBody
    @RequestMapping("getwode")
    public JsonResult getwode(@RequestParam Map<String, Object> params) {

        return ifationservice.getwode(params);

    }

    /**
     * 新增或者修改学历信息
     *
     * @param params
     * @return
     */
    @ResponseBody
    @RequestMapping("saveschool")
    public JsonResult saveschool(@org.springframework.web.bind.annotation.RequestBody Map<String, Object> params) {

        return ifationservice.saveschool(params);
    }

    /**
     * 查询个人学历信息
     *
     * @param params
     * @return
     */
    @ResponseBody
    @RequestMapping("getschool")
    public JsonResult getschool(@RequestParam Map<String, Object> params) {

        return ifationservice.getschool(params);

    }


    /**
     * 查询个人银行卡信息
     *
     * @param params
     * @return
     */
    @ResponseBody
    @RequestMapping("getbankByid")
    public JsonResult getbankByid(@RequestParam Map<String, Object> params) {

        return ifationservice.getbankByid(params);

    }


    /**
     * 新增或者修改银行卡信息
     *
     * @param params
     * @return
     */
    @ResponseBody
    @RequestMapping("savebank")
    public JsonResult savebank(@org.springframework.web.bind.annotation.RequestBody Map<String, Object> params) {

        return ifationservice.savebank(params);
    }


    /**
     * 查询是否设置了服务密码
     *
     * @param params
     * @return
     */
    @ResponseBody
    @RequestMapping("gettenantTenSerpad")
    public JsonResult gettenantTenSerpad(@RequestParam Map<String, Object> params) {

        return ifationservice.gettenantTenSerpad(params);

    }

    /**
     * 根据id 查询手机号码
     *
     * @param params
     * @return
     */
    @ResponseBody
    @RequestMapping("getusrTenPhonebyid")
    public JsonResult getusrTenPhonebyid(@RequestParam Map<String, Object> params) {

        return ifationservice.getusrTenPhonebyid(params);

    }

    /**
     * 设置或者修改服务密码
     *
     * @param params
     * @return
     */
    @ResponseBody
    @RequestMapping("updateTenSerpadById")
    public JsonResult updateTenSerpadById(@RequestParam Map<String, Object> params) {

        return ifationservice.updateTenSerpadById(params);

    }

    /**
     * 查询是否这是了密保问题
     *
     * @param params
     * @return
     */
    @ResponseBody
    @RequestMapping("getencrypted")
    public JsonResult getencrypted(@RequestParam Map<String, Object> params) {

        return ifationservice.getencrypted(params);

    }


    /**
     * 查询密保问题的题目
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/getquestion")
    public JsonResult getquestion() {

        return developmentFeignClient.getquestion();
    }

    /**
     * 查询是否这是了密保问题
     *
     * @param params
     * @return
     */
    @ResponseBody
    @RequestMapping("saveencrypted")
    public JsonResult saveencrypted(@org.springframework.web.bind.annotation.RequestBody Map<String, Object> params) {

        return ifationservice.saveencrypted(params);

    }

    /**
     * 用户新增对唐巣的建议
     *
     * @param params
     * @return
     */
    @ResponseBody
    @RequestMapping("savesuggest")
    public JsonResult savesuggest(@org.springframework.web.bind.annotation.RequestBody Map<String, Object> params) {

        return ifationservice.savesuggest(params);

    }

    /**
     * 查询系统消息列表
     *
     * @param params
     * @return
     */
    @ResponseBody
    @RequestMapping("getSysmsgById")
    public JsonResult getSysmsgById(@RequestParam Map<String, Object> params) {

        return ifationservice.getSysmsgById(params);

    }

    /**
     * 查看一个系统详情
     *
     * @param params
     * @return
     */
    @ResponseBody
    @RequestMapping("getSysmsgInfoById")
    public JsonResult getSysmsgInfoById(@RequestParam Map<String, Object> params) {

        return ifationservice.getSysmsgInfoById(params);

    }

    /**
     * 删除一个系统消息
     *
     * @param params
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteSysmsgById")
    public JsonResult deleteSysmsgById(@RequestParam Map<String, Object> params) {

        return ifationservice.deleteSysmsgById(params);

    }

    /**
     * 移除一个喜爱
     *
     * @param params
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteLikeById")
    public JsonResult deleteLikeById(@RequestParam Map<String, Object> params) {

        return ifationservice.deleteLikeById(params);

    }

    /**
     * 查询我的收藏列表
     *
     * @param params
     * @return
     */
    @ResponseBody
    @RequestMapping("getTenantLikeById")
    public JsonResult getTenantLikeById(@RequestParam Map<String, Object> params) {

        return ifationservice.getTenantLikeById(params);

    }
}
